<?php

namespace app\xwam\model;

/**
 * Description of ConfigModel
 *
 * @version 1.0 2015-10-20 SoChishun(14507247@qq.com) Added.
 */
class SystemConfModel {

    const TABLE = 'system_conf';

    public function getConfData() {
        $list = Cache::store('redis_temp')->get('conf');
        if (!$list) {
            $list = db(self::TABLE)->column('conf_name, conf_value');
            Cache::store('redis_temp')->set('conf', $list, 10);
        }
        return $list;
    }

    public function getConfValue($name, $defv = '') {
        $list = $this->getConfData();
        return isset($list[$name]) ? $list[$name] : $defv;
    }

    public function getListInGroup() {
        $list = db(self::TABLE)->select();
        $outList = [];
        foreach ($list as $row) {
            $outList[$row['group_text']][] = $row;
        }
        return $outList;
    }

    public function saveRecords($data) {
        foreach ($data as $name => $value) {
            db(self::TABLE)->where('conf_name', $name)->setField('conf_value', $value);
        }
    }

}
